package com.aishua.fire.common;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.aishua.fire.common.constant.SessionConstants;
import com.aishua.fire.jpa.AccountDao;
import com.aishua.fire.jpa.entity.Account;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class RequestInterfaceUtil {

	@Autowired
	private AccountDao accountDao;
	
	public Account getAccount() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    	log.info("service层获取supplierID时的attributes值是:{}",attributes);
		HttpServletRequest request = attributes.getRequest();
    	String userName = (String) request.getAttribute(SessionConstants.JWT_USERNAME);
		return accountDao.getByUserName(userName);
	}
	
	public Long getAccountId() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    	log.info("service层获取supplierID时的attributes值是:{}",attributes);
		HttpServletRequest request = attributes.getRequest();
    	String userName = (String) request.getAttribute(SessionConstants.JWT_USERNAME);
    	Long id = accountDao.findLoginIdByUserName(userName);
		return id;
	}
	
	public String getUserName() {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    	log.info("service层获取supplierID时的attributes值是:{}",attributes);
		HttpServletRequest request = attributes.getRequest();
    	String userName = (String) request.getAttribute(SessionConstants.JWT_USERNAME);
    	return userName;
	}
}
